package excel.export;

import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Workbook;

import com.mvc.core.utils.excel.ExcelUtils;
import com.mvc.core.utils.excel.ExportRules;
import com.mvc.core.utils.excel.ExportSheetCallback;
import com.mvc.core.utils.excel.POIConstant;



public class MainClass {

	public static void main(String[] args) {
		try {
			exportBeanWithNum2();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public static void exportBeanWithNum() throws Exception {
		List<ProjectEvaluate> data = new ArrayList<ProjectEvaluate>();
		ProjectEvaluate obj = new ProjectEvaluate();
		obj.setProjectName("中青旅");
		obj.setAreaName("华东长三角");
		obj.setProvince("河北省");
		obj.setCity("保定市");
		obj.setStatusName("已签署协议");
		obj.setScount("9.55");
		obj.setAreaScore(1.2332);
		obj.setResourceScore(2.4232);
		obj.setManageScore(3.2323);
		obj.setResourceScore(4.0323);
		obj.setReviewScore(5.2323);
		obj.setTeamScore(6.0234);
		obj.setPotentialScore(21232133.0423);
		obj.setCreateTime(new Date());
		data.add(obj);
		ProjectEvaluate obj2 = new ProjectEvaluate();
		obj2.setProjectName("亚信");
		obj2.setAreaName("华北京津冀");
		obj2.setProvince("山西省");
		obj2.setCity("太原市");
		obj2.setStatusName("已终止");
		obj2.setScount("12.38");
		obj2.setAreaScore(7.2332);
		obj2.setResourceScore(8.4232);
		obj2.setManageScore(9.2323);
		obj2.setResourceScore(11.0323);
		obj2.setReviewScore(52.2323);
		obj2.setTeamScore(65.0234);
		obj2.setPotentialScore(21232133.0423);
		obj2.setCreateTime(new Date());

		for (int i = 0; i < 10; i++) {
			data.add(obj2);
		}

		// 表头设置
		HashMap<String, String> rules = new HashMap<>();
		rules.put("1,1,A,M", "项目资源统计");
		rules.put("2,3,A,A", "序号");
		rules.put("2,3,B,B", "项目名称");
		rules.put("2,3,C,C", "所属区域");
		rules.put("2,3,D,D", "省份");
		rules.put("2,3,E,E", "市");
		rules.put("2,3,F,F", "项目状态");
		rules.put("2,3,G,G", "总分");
		rules.put("2,2,H,M", "单项评分");
		rules.put("3,3,H,H", "区位条件");
		rules.put("3,3,I,I", "资源禀赋");
		rules.put("3,3,J,J", "经营现状");
		rules.put("3,3,K,K", "考察印象");
		rules.put("3,3,L,L", "管理团队");
		rules.put("3,3,M,M", "创建时间");
		// 尾部设置
		HashMap<String, String> footerRules = new HashMap<>();
		footerRules.put("1,2,A,C", "注释:");
		footerRules.put("1,2,D,M", "导出参考代码！");
		// 导出字段设置
		Object[][] fileds = { { "projectName", POIConstant.NUMBER }, { "areaName", POIConstant.NUMBER }, { "province", POIConstant.NUMBER }, { "city", POIConstant.NAME },
				{ "statusName", POIConstant.NUMBER }, { "scount", POIConstant.NUMBER }, { "areaScore", POIConstant.NUMBER }, { "resourceScore", POIConstant.NUMBER },
				{ "manageScore", POIConstant.NUMBER }, { "reviewScore", POIConstant.NUMBER }, { "teamScore", POIConstant.NUMBER }, { "createTime", POIConstant.charWidth(10) } };
		Workbook bean = ExcelUtils.createWorkbook(data, new ExportRules(true, fileds, rules, footerRules));
		bean.write(new FileOutputStream("src/test/java/example/exp/export.xlsx"));
	}

	public static void exportBeanWithNum2() throws Exception {
		List<ProjectEvaluate> data = new ArrayList<ProjectEvaluate>();

		for (int i = 0; i < 20; i++) {
			ProjectEvaluate obj = new ProjectEvaluate();
			obj.setProjectName("中青旅" + i);
			obj.setAreaName("华东长三角");
			obj.setProvince("河北省");
			obj.setCity("保定市");
			obj.setStatusName("已签署协议");
			obj.setScount("9.55");
			obj.setAreaScore(1.2332);
			obj.setResourceScore(2.4232);
			obj.setManageScore(3.2323);
			obj.setResourceScore(4.0323);
			obj.setReviewScore(5.2323);
			obj.setTeamScore(6.0234);
			obj.setPotentialScore(21232133.0423);
			obj.setCreateTime(new Date());
			data.add(obj);
		}
		String title = "项目资源统计";
		String[] hearder = { "项目名称", "所属区域", "省份", "市", "项目状态", "A", "B", "C", "D", "E", "F", "G" };
		// 导出字段设置
		Object[][] fileds = { { "projectName", POIConstant.NUMBER }, { "areaName", POIConstant.NUMBER }, { "province", POIConstant.NUMBER }, { "city", POIConstant.NAME },
				{ "statusName", POIConstant.NUMBER }, { "scount", POIConstant.NUMBER }, { "areaScore", POIConstant.NUMBER }, { "resourceScore", POIConstant.NUMBER },
				{ "manageScore", POIConstant.NUMBER }, { "reviewScore", POIConstant.NUMBER }, { "teamScore", POIConstant.NUMBER }, { "createTime", POIConstant.charWidth(10) } };
		Workbook bean = ExcelUtils.createWorkbook(data, new ExportRules(false, fileds, title, hearder, null), new ExportSheetCallback() {

			@Override
			public Object callback(String fieldName, Object value, CellStyle style) throws Exception {
				if (fieldName.equals("createTime")) {
					// 创建日期修改
					value = new SimpleDateFormat(POIConstant.FMTDATE).format((Date) (value));
					// 居右
					style.setAlignment(CellStyle.ALIGN_RIGHT);
				}
				return value;
			}

		});
		bean.write(new FileOutputStream("src/test/java/example/exp/export.xlsx"));
	}

}
